﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media.Effects;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Ink;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace TouchUmlUI
{   
 
    public class OurCanvas : InkCanvas
    {
        public ArrayList al = new ArrayList();
        private ScrollViewer sv;                
        public Diagram.ModelDiagramu model;        
        Window1 windowParent;
       
        public void setWindowParent(Window1 windowParent)
        {
            this.windowParent = windowParent;
        }

        public UMLObj findUMLObjByName(String findName)
        {
            UMLObj ret = null;
            foreach (UMLObj aobj in al)
            {
                if (aobj.guml.obj_uml == null) continue;
                if (aobj.guml.obj_uml.Nazwa == null) continue;
                    if (aobj.guml.obj_uml.Nazwa.CompareTo(findName)==0)
                        ret = aobj;
            }
            return ret;
        }

        public void usunObiektUML(UMLObj umlObj)
        {
            windowParent.treeModelView.listBox.Items.Clear();

            /* usuwanie pustych asocjacji */
            if (umlObj is AssociableObj)
            {
                AssociableObj aObj = umlObj as AssociableObj;
                foreach (AssociationObj ao in aObj.associationObj)
                {
                    ao.guml.panel.Children.Clear();
                    ao.guml.canvas.al.Remove(ao);
                    ao.guml.canvas.Children.Remove(ao.guml.panel);
                    model.UsunElementZModelu(ao.guml.obj_uml);
                    ao.guml.canvas.UpdateLayout();
                }
            }

            al.Remove(umlObj);
            umlObj.guml.panel.Children.Clear();
            umlObj.guml.panel.Background = Brushes.Transparent;
            this.Children.Remove(umlObj.guml.panel);
            //model.UsunElementZModelu(umlObj.guml.obj_uml);
            model.UsunElementZDiagramu((Diagram.ElementNaDiagramie)umlObj.guml.obj_uml);
            umlObj.guml.canvas.UpdateLayout();

            /*foreach (OurTab tab in windowParent.tabbedControl.Tabs)
            {
                foreach (UMLObj obj in ((tab.canvas as OurCanvas).al))
                {
                    if (obj is AssociationObj)
                    {
                        AssociationObj aObj = obj as AssociationObj;
                        if ((aObj.fromRef.Equals(umlObj)) || (aObj.toRef.Equals(umlObj)))
                        {
                            MessageBox.Show("znalazlem asocjacje: " + aObj);
                            al.Remove(aObj);
                            this.Children.Remove(aObj.guml.panel);
                            model.UsunElementZModelu(aObj.guml.obj_uml);

                        }
                    }
                }
            }*/
            windowParent.treeModelView.Refresh();
        }

        public OurCanvas(Window1 windowParent)           
        {
            setWindowParent(windowParent);
            this.EditingMode = InkCanvasEditingMode.Ink;
            model = new Diagram.ModelDiagramu("model");
            //OurCanvas.MouseDownEvent = EventManager.RegisterRoutedEvent("MouseDown",RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Button));
            /*
            if (IsGestureRecognizerAvailable)
            {
                //Console.WriteLine("GestureRecognizerAvailable!");
                EditingMode = InkCanvasEditingMode.InkAndGesture;
                Gesture += new InkCanvasGestureEventHandler(windowParent.OnGesture);
                SetEnabledGestures(new ApplicationGesture[] 
                    {
                     ApplicationGesture.Left,
                     ApplicationGesture.Right,
                  });
            }
            else
            {
                //Console.WriteLine("GestureRecgnizer not Available!!!!");
            }*/

           // Width = 1024;
            //Height = 768;



        }

        public ScrollViewer getScrollViewer()
        {
            return sv;
        }

        public void setScrollViewer(ScrollViewer a_sv)
        {
            sv = a_sv;
        }

       /* public void showGrid(Visibility a_show)
        {
            if (a_show == Visibility.Visible)
            {
                if (grid == null) grid = new Grid(this);
                grid.showGrid(a_show);
            }
            else
            {
                if (grid != null)
                {
                    grid.showGrid(a_show);
                }
            }
        }
*/
        public UMLObj dodajAsocjacje(UMLType a_wybor, AssociableObj ref1, AssociableObj ref2, GUML aguml)
        {
            UMLObj ret = null;
            if (a_wybor == UMLType.ASSOCIATION)
            {
                AssociationObj aObj = new AssociationObj(ref1, ref2,aguml,this);
                ret = aObj;
                ref1.calculateNewPoints();
                ref2.calculateNewPoints();
                ref1.updateDependencyProperties();
                ref2.updateDependencyProperties();
                al.Add(aObj);                
            }
            return ret;
        }        

        public UMLObj dodajObiekt(UMLType a_wybor, Polozenie a_polozenie, String a_name, GUML aguml,bool addObj)
        {
            UMLObj ret = null;
            if (a_wybor == UMLType.CLASS)
            {
                KlasaObj kobj = new KlasaObj(KlasaObj.OBJ.CLASS,a_name, a_polozenie.width, a_polozenie.height, a_polozenie.x, a_polozenie.y, this, al.Count, aguml);                
                kobj.Refresh();
                ret = kobj;               
            }
            else if (a_wybor == UMLType.INTERFACE)
            {
                KlasaObj kobj = new KlasaObj(KlasaObj.OBJ.INTERFACE, a_name, a_polozenie.width, a_polozenie.height, a_polozenie.x, a_polozenie.y, this, al.Count, aguml);
                kobj.Refresh();
                ret = kobj;
            }
            else if (a_wybor == UMLType.NOTICE)
            {
                NotatkaObj nobj = new NotatkaObj(a_name, a_polozenie.width, a_polozenie.height, a_polozenie.x, a_polozenie.y, this, al.Count, aguml);
                ret = nobj;              
            }
            else if (a_wybor == UMLType.ACTION)
            {
                AkcjaObj aobj = new AkcjaObj(a_name, a_polozenie.width, a_polozenie.height, a_polozenie.x, a_polozenie.y, this, al.Count, aguml);
                ret = aobj;               
            }
            else if (a_wybor == UMLType.ACTOR)
            {
                AktorObj aobj = new AktorObj(a_name, a_polozenie.width, a_polozenie.height, a_polozenie.x, a_polozenie.y, this, al.Count, aguml);
                ret = aobj;         
            }

            if (addObj)
            {
                al.Add(ret);
                ret.updateDependencyProperties();
            }

            if (al.Count > 1)
            {
                al.Sort(new Cmp());
            }

            return ret;

        }
    }
}
